import db from '../db/index.js'
export function getrole(req, res) {
    const { account, role } = req.body
    function getid(role) {
        let id = ''
        switch (role) {
            case 'student': return id = 'stuid';
            case 'teacher': return id = 'teacherid';
            case 'admin': return id = 'adminid';
        }
        return id
    }
    const id = getid(role)
    const sql1 = `select * from ${role} where ${id}=?`
    db.query(sql1, [account], (err, data) => {
        if (err) return res.dbno(err)
        if (data.length === 0) return res.cc(198, '账号不存在')
        const user = data[0]
        const sql2 = 'select * from role where roleid  = ?'
        db.query(sql2, user.roleid, (err, data) => {            
            if (err) return res.dbno(err)
            const permissionsid = JSON.parse(data[0].permissions)
            const subperid = JSON.parse(data[0].subper)
            const sql3 = 'select permissionid,name,path,type,title from permission where permissionid in (?)  and id in (?) ' 
            db.query(sql3, [permissionsid,subperid], (err, data) => {
                if (err) return res.dbno(err)
                res.send({ status: 200, data })
            })
        })
    })

}